#pragma once

#include <string>
#include <jsoncpp/json/json.h>

namespace WordMemory
{
    struct CommunityPost
    {
        int id;
        int user_id;
        std::string title;
        std::string content;
        std::string created_at;
        std::string updated_at;

        std::string author_username;
        int likes_count = 0;
        int shares_count = 0;
        bool is_liked_by_current_user = false; // 当前登录用户是否点赞了此帖

        CommunityPost(int id = 0,
                      int user_id = 0,
                      const std::string& title = "",
                      const std::string& content = "",
                      const std::string& created_at = "",
                      const std::string& updated_at = "",
                      const std::string& author_username = "未知作者",
                      int likes_count = 0,
                      int shares_count = 0,
                      bool is_liked_by_current_user = false)
            : id(id)
            , user_id(user_id)
            , title(title)
            , content(content)
            , created_at(created_at)
            , updated_at(updated_at)
            , author_username(author_username)
            , likes_count(likes_count)
            , shares_count(shares_count)
            , is_liked_by_current_user(is_liked_by_current_user)
        {}

        Json::Value to_json() const
        {
            Json::Value json;
            json["id"] = id;
            json["user_id"] = user_id;
            json["title"] = title;
            json["content"] = content;
            json["created_at"] = created_at;
            json["updated_at"] = updated_at;
            json["author_username"] = author_username;
            json["likes_count"] = likes_count;
            json["shares_count"] = shares_count;
            json["is_liked_by_current_user"] = is_liked_by_current_user;
            return json;
        }

        void from_json(const Json::Value& json)
        {
            id = json.get("id", 0).asInt();
            user_id = json["user_id"].asInt();
            title = json["title"].asString();
            content = json["content"].asString();
            created_at = json.get("created_at", "").asString();
            updated_at = json.get("updated_at", "").asString();
            author_username = json.get("author_username", "未知作者").asString();
            likes_count = json.get("likes_count", 0).asInt();
            shares_count = json.get("shares_count", 0).asInt();
            is_liked_by_current_user = json.get("is_liked_by_current_user", false).asBool();
        }
    };
}